package simple;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @author ：zhz
 * @date ：Created in 2021/02/09
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 同步发送消息
 **/
public class SyncProducer {
    public static void main(String[] args) throws MQClientException {
        // 1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
        // 2.指定NameServer地址
        producer.setNamesrvAddr("localhost:9876");
        // 3.启动producer
        producer.start();

        for (int i = 0; i < 20; i++) {
            try {
                // 4.创建消息对象，指定主题Topic、Tag和消息体
                Message message = new Message("TopicTest",
                        "TagA",
                        "OrderID100",
                        "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                // 5.发送消息
                producer.sendOneway(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 6.关闭生产者producer
        producer.shutdown();
    }
}
